<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>checkboxrenderer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_ui_checkboxrenderer.js.html">checkboxrenderer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Default renderer for {@link goog.ui.Checkbox}s.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.ui.CheckboxRenderer&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line23"></a>goog.require(&#39;goog.a11y.aria.Role&#39;);
<a name="line24"></a>goog.require(&#39;goog.a11y.aria.State&#39;);
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.classlist&#39;);
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.ControlRenderer&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Default renderer for {@link goog.ui.Checkbox}s.  Extends the superclass
<a name="line35"></a> * to support checkbox states:
<a name="line36"></a> * @constructor
<a name="line37"></a> * @extends {goog.ui.ControlRenderer}
<a name="line38"></a> */
<a name="line39"></a>goog.ui.CheckboxRenderer = function() {
<a name="line40"></a>  goog.ui.CheckboxRenderer.base(this, &#39;constructor&#39;);
<a name="line41"></a>};
<a name="line42"></a>goog.inherits(goog.ui.CheckboxRenderer, goog.ui.ControlRenderer);
<a name="line43"></a>goog.addSingletonGetter(goog.ui.CheckboxRenderer);
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Default CSS class to be applied to the root element of components rendered
<a name="line48"></a> * by this renderer.
<a name="line49"></a> * @type {string}
<a name="line50"></a> */
<a name="line51"></a>goog.ui.CheckboxRenderer.CSS_CLASS = goog.getCssName(&#39;goog-checkbox&#39;);
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>/** @override */
<a name="line55"></a>goog.ui.CheckboxRenderer.prototype.createDom = function(checkbox) {
<a name="line56"></a>  var element = checkbox.getDomHelper().createDom(
<a name="line57"></a>      &#39;span&#39;, this.getClassNames(checkbox).join(&#39; &#39;));
<a name="line58"></a>
<a name="line59"></a>  var state = checkbox.getChecked();
<a name="line60"></a>  this.setCheckboxState(element, state);
<a name="line61"></a>
<a name="line62"></a>  return element;
<a name="line63"></a>};
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/** @override */
<a name="line67"></a>goog.ui.CheckboxRenderer.prototype.decorate = function(checkbox, element) {
<a name="line68"></a>  // The superclass implementation takes care of common attributes; we only
<a name="line69"></a>  // need to set the checkbox state.
<a name="line70"></a>  element = goog.ui.CheckboxRenderer.base(this, &#39;decorate&#39;, checkbox, element);
<a name="line71"></a>  goog.asserts.assert(element);
<a name="line72"></a>  var classes = goog.dom.classlist.get(element);
<a name="line73"></a>  // Update the checked state of the element based on its css classNames
<a name="line74"></a>  // with the following order: undetermined -&gt; checked -&gt; unchecked.
<a name="line75"></a>  var checked = /** @suppress {missingRequire} */ (
<a name="line76"></a>      goog.ui.Checkbox.State.UNCHECKED);
<a name="line77"></a>  if (goog.array.contains(classes,
<a name="line78"></a>      this.getClassForCheckboxState(
<a name="line79"></a>          /** @suppress {missingRequire} */
<a name="line80"></a>          goog.ui.Checkbox.State.UNDETERMINED))) {
<a name="line81"></a>    checked = (/** @suppress {missingRequire} */
<a name="line82"></a>        goog.ui.Checkbox.State.UNDETERMINED);
<a name="line83"></a>  } else if (goog.array.contains(classes,
<a name="line84"></a>      this.getClassForCheckboxState(
<a name="line85"></a>          /** @suppress {missingRequire} */ goog.ui.Checkbox.State.CHECKED))) {
<a name="line86"></a>    checked = /** @suppress {missingRequire} */ goog.ui.Checkbox.State.CHECKED;
<a name="line87"></a>  } else if (goog.array.contains(classes,
<a name="line88"></a>      this.getClassForCheckboxState(/** @suppress {missingRequire} */
<a name="line89"></a>          goog.ui.Checkbox.State.UNCHECKED))) {
<a name="line90"></a>    checked = (/** @suppress {missingRequire} */
<a name="line91"></a>        goog.ui.Checkbox.State.UNCHECKED);
<a name="line92"></a>  }
<a name="line93"></a>  checkbox.setCheckedInternal(checked);
<a name="line94"></a>  goog.asserts.assert(element, &#39;The element cannot be null.&#39;);
<a name="line95"></a>  goog.a11y.aria.setState(element, goog.a11y.aria.State.CHECKED,
<a name="line96"></a>      this.ariaStateFromCheckState_(checked));
<a name="line97"></a>
<a name="line98"></a>  return element;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Returns the ARIA role to be applied to checkboxes.
<a name="line104"></a> * @return {goog.a11y.aria.Role} ARIA role.
<a name="line105"></a> * @override
<a name="line106"></a> */
<a name="line107"></a>goog.ui.CheckboxRenderer.prototype.getAriaRole = function() {
<a name="line108"></a>  return goog.a11y.aria.Role.CHECKBOX;
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Updates the appearance of the control in response to a checkbox state
<a name="line114"></a> * change.
<a name="line115"></a> * @param {Element} element Checkbox element.
<a name="line116"></a> * @param {goog.ui.Checkbox.State} state Updated checkbox state.
<a name="line117"></a> */
<a name="line118"></a>goog.ui.CheckboxRenderer.prototype.setCheckboxState = function(
<a name="line119"></a>    element, state) {
<a name="line120"></a>  if (element) {
<a name="line121"></a>    goog.asserts.assert(element);
<a name="line122"></a>    var classToAdd = this.getClassForCheckboxState(state);
<a name="line123"></a>    goog.asserts.assert(classToAdd);
<a name="line124"></a>    goog.asserts.assert(element);
<a name="line125"></a>    if (goog.dom.classlist.contains(element, classToAdd)) {
<a name="line126"></a>      return;
<a name="line127"></a>    }
<a name="line128"></a>    goog.object.forEach(
<a name="line129"></a>        /** @suppress {missingRequire} */ goog.ui.Checkbox.State,
<a name="line130"></a>        function(state) {
<a name="line131"></a>          var className = this.getClassForCheckboxState(state);
<a name="line132"></a>          goog.asserts.assert(element);
<a name="line133"></a>          goog.dom.classlist.enable(element, className,
<a name="line134"></a>                                    className == classToAdd);
<a name="line135"></a>        }, this);
<a name="line136"></a>    goog.a11y.aria.setState(element, goog.a11y.aria.State.CHECKED,
<a name="line137"></a>        this.ariaStateFromCheckState_(state));
<a name="line138"></a>  }
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Gets the checkbox&#39;s ARIA (accessibility) state from its checked state.
<a name="line144"></a> * @param {goog.ui.Checkbox.State} state Checkbox state.
<a name="line145"></a> * @return {string} The value of goog.a11y.aria.state.CHECKED. Either &#39;true&#39;,
<a name="line146"></a> *     &#39;false&#39;, or &#39;mixed&#39;.
<a name="line147"></a> * @private
<a name="line148"></a> */
<a name="line149"></a>goog.ui.CheckboxRenderer.prototype.ariaStateFromCheckState_ = function(state) {
<a name="line150"></a>  if (state ==
<a name="line151"></a>      /** @suppress {missingRequire} */ goog.ui.Checkbox.State.UNDETERMINED) {
<a name="line152"></a>    return &#39;mixed&#39;;
<a name="line153"></a>  } else if (state ==
<a name="line154"></a>             /** @suppress {missingRequire} */ goog.ui.Checkbox.State.CHECKED) {
<a name="line155"></a>    return &#39;true&#39;;
<a name="line156"></a>  } else {
<a name="line157"></a>    return &#39;false&#39;;
<a name="line158"></a>  }
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/** @override */
<a name="line163"></a>goog.ui.CheckboxRenderer.prototype.getCssClass = function() {
<a name="line164"></a>  return goog.ui.CheckboxRenderer.CSS_CLASS;
<a name="line165"></a>};
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/**
<a name="line169"></a> * Takes a single {@link goog.ui.Checkbox.State}, and returns the
<a name="line170"></a> * corresponding CSS class name.
<a name="line171"></a> * @param {goog.ui.Checkbox.State} state Checkbox state.
<a name="line172"></a> * @return {string} CSS class representing the given state.
<a name="line173"></a> * @protected
<a name="line174"></a> */
<a name="line175"></a>goog.ui.CheckboxRenderer.prototype.getClassForCheckboxState = function(state) {
<a name="line176"></a>  var baseClass = this.getStructuralCssClass();
<a name="line177"></a>  if (state ==
<a name="line178"></a>      /** @suppress {missingRequire} */ goog.ui.Checkbox.State.CHECKED) {
<a name="line179"></a>    return goog.getCssName(baseClass, &#39;checked&#39;);
<a name="line180"></a>  } else if (state ==
<a name="line181"></a>             /** @suppress {missingRequire} */
<a name="line182"></a>             goog.ui.Checkbox.State.UNCHECKED) {
<a name="line183"></a>    return goog.getCssName(baseClass, &#39;unchecked&#39;);
<a name="line184"></a>  } else if (state ==
<a name="line185"></a>             /** @suppress {missingRequire} */
<a name="line186"></a>             goog.ui.Checkbox.State.UNDETERMINED) {
<a name="line187"></a>    return goog.getCssName(baseClass, &#39;undetermined&#39;);
<a name="line188"></a>  }
<a name="line189"></a>  throw Error(&#39;Invalid checkbox state: &#39; + state);
<a name="line190"></a>};
</pre>


</body>
</html>
